import {
	baseOssUrl,
	baseOssUrl2
} from '@/common/settings';
export default {
	data() {
		return {
			slNum:'',
			orderIddd:'', // 下单id
			pricebn:'',
			num:'',
			orderId:'',
			id: 0,
			form:{}, //商品详情
			addForm: {
				zName: '', //姓名
				phone:'', // 手机号
				// shdz: '', // 收获地址
				spId: '', // 商品id
				// sl:'1', // 数量
				userId: '', // 用户id
				version: '', //版本号
			}, //下单
			baseOssUrl: baseOssUrl,
			baseOssUrl2: baseOssUrl2,
		}
	},
	onPageScroll(e) {

	},
	computed: {

	},
	watch: {

	},
	onShow() {
	},
	onLoad(option) {
		this.id = option.id ? option.id : 0
		this.detail()
	},
	// 上拉触底
	onReachBottom() {

	},
	// 下拉刷新
	onPullDownRefresh() {

	},

	methods: {
		//跳转选择地址
		navto() {
			this.$tools.navigate('/pagesA/user/address/index?choice=1')
		},
		changes(e) {
			this.num =  e.value
			this.pricebn =  parseFloat(e.value * this.form.dj).toFixed(2)
			// if(e.value > this.slNum) {
			// 	uni.showToast({
			// 		icon: 'none',
			// 		title: '数量不够!',
			// 		duration: 2000
			// 	});
			// 	return
				
			// } else {
			// 	this.num =  e.value
			// 	this.pricebn =  parseFloat(e.value * this.form.dj).toFixed(2)
			// }
			console.log(e,'这是什么什么什么')
		
		},
		// 获取商品详情
		detail() {
			this.$http.get(this.$httpApi.dyInfo, {
				data: {
					 id: this.id,
				}
			}).then(res => {
				if (res.code == 0) {
					console.log(res,'resss');
					this.form = res.dyb
					// this.pricebn = res.creativeGoods.dj
					// this.slNum =  res.creativeGoods.sl
				}
			})
		},
		submin() {
			if(!this.addForm.zName) {
				uni.showToast({
					icon: 'none',
					title: '请输入订购人姓名!',
					duration: 2000
				});
				return
			}
			if(!this.addForm.phone) {
				uni.showToast({
					icon: 'none',
					title: '请输入收货人手机号!',
					duration: 2000
				});
				return
			}
			if(this.addForm.phone.lenght < 11) {
				uni.showToast({
					icon: 'none',
					title: '请输入正确的收货人手机号!',
					duration: 2000
				});
				return
			}
			// if(!this.addForm.shdz) {
			// 	uni.showToast({
			// 		icon: 'none',
			// 		title: '请输入收货地址!',
			// 		duration: 2000
			// 	});
			// 	return
			// }
			// if(this.num > this.form.sl){
			// 	uni.showToast({
			// 		icon: 'none',
			// 		title: '购买数量超出了库存!',
			// 		duration: 2000
			// 	});
			// 	return
			// }
			this.$http.get(this.$httpApi.dySave,{
				data: {
					zName: this.addForm.zName,
					// sl: this.num,
					phone:this.addForm.phone,
					userId:uni.getStorageSync('userinfo').userId,
					spId: this.id,
					// shdz:this.addForm.shdz,
					version: this.form.version	
				}
			}).then(res => {
				if (res.code == 0) {
					this.orderId = res.orderId
					this.$http.post(this.$httpApi.ordeWxPay + "?userId=" + uni.getStorageSync('userinfo').userId + "&number=" + res.number).then(res => {
						this.orderIddd = res.orderId
						if (res.code == 0) {
							uni.requestPayment({
								provider: 'wxpay',
								timeStamp: res.response.timeStamp,
								nonceStr: res.response.nonceStr,
								package: 'prepay_id=' + res.response.prepayId,
								signType: 'MD5',
								paySign: res.response.paySign,
								success: res => {
									uni.showToast({
										title: '购买成功！',
										duration: 2000
									});
									setTimeout(res=>{
										uni.redirectTo({
											url: '/pages/writing/scenic/index?id=' + this.orderIddd
										})
									},1000)
								},
								fail: err => {
									console.log('fail:' + JSON.stringify(err));
								}
							});
						}
					})
					
				}
			})
			
		},
	}
}
